#include "PlayerSelectDlg.h"
#include "ui_PlayerSelectDlg.h"
#include <QCloseEvent>
#include <QApplication>
#include <QDebug>

PlayerSelectDlg::PlayerSelectDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PlayerSelectDlg),
    mSelectedPlayer("")
{
    ui->setupUi(this);
    setWindowIcon(QIcon(QPixmap("icon.png")));
    setWindowTitle("Choose the first player");
    connect(ui->player1Btn, SIGNAL(clicked()), this, SLOT(onPlayerSelected()));
    connect(ui->player2Btn, SIGNAL(clicked()), this, SLOT(onPlayerSelected()));
}

PlayerSelectDlg::~PlayerSelectDlg()
{
    delete ui;
}

void PlayerSelectDlg::closeEvent(QCloseEvent *e)
{
  reject();
}

void PlayerSelectDlg::setPlayers(const QString &player1, const QString &player2)
{
  ui->player1Btn->setText(player1);
  ui->player2Btn->setText(player2);
}

void PlayerSelectDlg::onPlayerSelected()
{
  QPushButton *p(dynamic_cast<QPushButton*>(sender()));
  if (p != NULL)
  {
    mSelectedPlayer = p->text();
    accept();
  }
}

QString PlayerSelectDlg::getFirstPlayer(QWidget *parent, const QString &p1, const QString &p2)
{
  PlayerSelectDlg psd(parent);
  psd.setPlayers(p1, p2);
  psd.raise();
  if (psd.exec() == QDialog::Accepted)
  {
    return psd.mSelectedPlayer;
  }
  else
  {
    return "";
  }
}
